#
# PlanetValuesTest
# Unit tests for the Planet Value and the Planet Value Event Handler
#

from CvPythonExtensions import *

gc = CyGlobalContext()

import unittest

# Tests planet value changes
class PlanetValueChangeTests(unittest.TestCase):
    def setUp(self):
        gc.getGame().setPlanetValue(0)

    def testCanChangePlanetValue(self):
        gc.getGame().changePlanetValue(2)
        self.assertEquals(gc.getGame().getPlanetValue(), 2)
        gc.getGame().changePlanetValue(-3)
        self.assertEquals(gc.getGame().getPlanetValue(), -1)

    def testCanSetPlanetValue(self):
        gc.getGame().setPlanetValue(2)
        self.assertEquals(gc.getGame().getPlanetValue(), 2)
        gc.getGame().setPlanetValue(-3)
        self.assertEquals(gc.getGame().getPlanetValue(), -3)
        

# Tests various registrations
class PlanetValueRegistrationTests(unittest.TestCase):
    def setUp(self):
        gc.getGame().unregisterAllPlanetEvents()

    def testCanRegisterName(self):
        gc.getGame().registerPlanetEvent("Test", 1, True, True, True)

    def testCannotRegisterTwiceName(self):
        gc.getGame().registerPlanetEvent("Test", 1, True, True, True)
        self.assertRaises(RuntimeError,
                          gc.getGame().registerPlanetEvent,
                          "Test", 1, True, True, True
                          )

    def testCanUnregisterName(self):
        gc.getGame().registerPlanetEvent("Test", 1, True, True, True)
        gc.getGame().unregisterPlanetEvent("Test")

    def testCannotUnregisterNonRegisteredName(self):
        self.assertRaises(RuntimeError,
                          gc.getGame().unregisterPlanetEvent,
                          "Test"
                          )

# Tests ability to get hit counts
class PlanetfallValueHitCountTests(unittest.TestCase):
    def setUp(self):
        gc.getGame().unregisterAllPlanetEvents()
        gc.getGame().setPlanetValue(0)

    def testRegisteredNameStartsWithZeroHits(self):
        gc.getGame().registerPlanetEvent("Test", 1, True, True, True)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 0)

    def testGetsOneHitCountWhenPassing(self):
        gc.getGame().registerPlanetEvent("Test", 1, True, True, True)
        gc.getGame().changePlanetValue(2)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 1)

    def testGetsOneHitCountWhenLandingOn(self):
        gc.getGame().registerPlanetEvent("Test", 1, True, True, True)
        gc.getGame().changePlanetValue(1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 1)

    def testsGetsNoHitCountWhenLandingBefore(self):
        gc.getGame().registerPlanetEvent("Test", 2, True, True, True)
        gc.getGame().changePlanetValue(1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 0)

    def testGetsNoHitCountWhenLandingStartFrom(self):
        gc.getGame().registerPlanetEvent("Test", 0, True, True, True)
        gc.getGame().changePlanetValue(1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 0)

    def testMultipleTestsGetsHitCount(self):
        gc.getGame().registerPlanetEvent("Test1", 1, True, True, True)
        gc.getGame().registerPlanetEvent("Test2", 1, True, True, True)
        gc.getGame().changePlanetValue(1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test1"), 1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test2"), 1)

    def testMultipleTestsRecieveCorrectHitCounts(self):
        gc.getGame().registerPlanetEvent("Test1", 1, True, True, True)
        gc.getGame().registerPlanetEvent("Test2", 2, True, True, True)
        gc.getGame().changePlanetValue(1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test1"), 1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test2"), 0)

# Tests ability to get hit counts only when ascending
class PlanetfallValueHitCountAscentTests(unittest.TestCase):
    def setUp(self):
        gc.getGame().unregisterAllPlanetEvents()
        gc.getGame().setPlanetValue(0)

    def testGetsHitCountOnAscent(self):
        gc.getGame().registerPlanetEvent("Test", 1, True, False, True)
        gc.getGame().changePlanetValue(1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 1)

    def testGetsNoHitCountOnOppositeAscent(self):
        gc.getGame().registerPlanetEvent("Test", -1, True, False, True)
        gc.getGame().changePlanetValue(-1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 0)

    def testOneGetsHitOneDoesNotOnAscent(self):
        gc.getGame().registerPlanetEvent("Test1", 1, True, False, True)
        gc.getGame().registerPlanetEvent("Test2", 1, False, True, True)
        gc.getGame().changePlanetValue(1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test1"), 1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test2"), 0)

# Tests ability to get hit counts only when descending
class PlanetfallValueHitCountDescentTests(unittest.TestCase):
    def setUp(self):
        gc.getGame().unregisterAllPlanetEvents()
        gc.getGame().setPlanetValue(0)

    def testGetsHitCountOnDescent(self):
        gc.getGame().registerPlanetEvent("Test", 1, True, False, True)
        gc.getGame().changePlanetValue(1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 1)

    def testGetsNoHitCountOnOppositeDescent(self):
        gc.getGame().registerPlanetEvent("Test", -1, True, False, True)
        gc.getGame().changePlanetValue(-1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 0)

    def testOneGetsHitOneDoesNotOnDescent(self):
        gc.getGame().registerPlanetEvent("Test1", 1, True, False, True)
        gc.getGame().registerPlanetEvent("Test2", 1, False, True, True)
        gc.getGame().changePlanetValue(1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test1"), 1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test2"), 0)

# Tests ability to get hit counts on multiple passes
class PlanetfallValueHitCountMultiPassTests(unittest.TestCase):
    def setUp(self):
        gc.getGame().unregisterAllPlanetEvents()
        gc.getGame().setPlanetValue(0)

    def testRepeatingEventGetsHitTwice(self):
        gc.getGame().registerPlanetEvent("Test", 1, True, True, True)
        gc.getGame().changePlanetValue(2)
        gc.getGame().changePlanetValue(-2)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 2)

    def testNonRepeatingEventGetsHitOnlyOnce(self):
        gc.getGame().registerPlanetEvent("Test", 1, True, True, False)
        gc.getGame().changePlanetValue(2)
        gc.getGame().changePlanetValue(-2)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 1)
        
# Tests ability to change hit count
class PlanetfallValueHitCountChangeTests(unittest.TestCase):
    def setUp(self):
        gc.getGame().unregisterAllPlanetEvents()
        gc.getGame().setPlanetValue(0)

    def testPlanetEventCanBeChanged(self):
        gc.getGame().registerPlanetEvent("Test", 1, True, True, True)
        gc.getGame().changePlanetValue(2)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 1)
        gc.getGame().changePlanetEventNewHitCount("Test", -1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 0)

    def testPlanetEventCannotBeChangedToNegative(self):
        gc.getGame().registerPlanetEvent("Test", 1, True, True, True)
        self.assertRaises(RuntimeError,
                          gc.getGame().changePlanetEventNewHitCount,
                          "Test", -1
                          )

# Tests ability to set hit count
class PlanetfallValueHitCountChangeTests(unittest.TestCase):
    def setUp(self):
        gc.getGame().unregisterAllPlanetEvents()
        gc.getGame().setPlanetValue(0)
        gc.getGame().registerPlanetEvent("Test", 1, True, True, True)

    def testPlanetEventCanBeSet(self):
        gc.getGame().setPlanetEventNewHitCount("Test", 1)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 1)

    def testPlanetEventCanBeSetToZero(self):
        gc.getGame().setPlanetEventNewHitCount("Test", 1)
        gc.getGame().setPlanetEventNewHitCount("Test", 0)
        self.assertEquals(gc.getGame().getPlanetEventNewHitCount("Test"), 0)

    def testPlanetEventCannotBeSetBelowZero(self):
        self.assertRaises(RuntimeError,
                          gc.getGame().setPlanetEventNewHitCount,
                          "Test", -1
                          )
        
        
